from typing import Any, Dict, Optional
import emails
from emails.template import JinjaTemplate
from app.core.config import settings

def send_email(
    email_to: str,
    subject_template: str = "",
    html_template: str = "",
    environment: Dict[str, Any] = {},
) -> None:
    """发送电子邮件"""
    assert settings.EMAILS_ENABLED, "no email settings provided"

    message = emails.Message(
        subject=JinjaTemplate(subject_template),
        html=JinjaTemplate(html_template),
        mail_from=(settings.EMAILS_FROM_NAME, settings.EMAILS_FROM_EMAIL),
    )

    smtp_options = {"host": settings.SMTP_HOST, "port": settings.SMTP_PORT}
    if settings.SMTP_TLS:
        smtp_options["tls"] = True
    if settings.SMTP_USER:
        smtp_options["user"] = settings.SMTP_USER
    if settings.SMTP_PASSWORD:
        smtp_options["password"] = settings.SMTP_PASSWORD

    response = message.send(
        to=email_to,
        render=environment,
        smtp=smtp_options,
    )

    return response

def send_reset_password_email(email_to: str, email: str, token: str) -> None:
    """发送重置密码邮件"""
    project_name = settings.PROJECT_NAME
    subject = f"{project_name} - Password recovery for user {email}"

    with open(settings.EMAIL_TEMPLATES_DIR / "reset_password.html") as f:
        template_str = f.read()

    server_host = settings.SERVER_HOST
    link = f"{server_host}/reset-password?token={token}"

    send_email(
        email_to=email_to,
        subject_template=subject,
        html_template=template_str,
        environment={
            "project_name": settings.PROJECT_NAME,
            "username": email,
            "email": email_to,
            "valid_hours": settings.EMAIL_RESET_TOKEN_EXPIRE_HOURS,
            "link": link,
        },
    )

def send_new_account_email(email_to: str, username: str, password: str) -> None:
    """发送新账户邮件"""
    project_name = settings.PROJECT_NAME
    subject = f"{project_name} - New account for user {username}"

    with open(settings.EMAIL_TEMPLATES_DIR / "new_account.html") as f:
        template_str = f.read()

    link = settings.SERVER_HOST

    send_email(
        email_to=email_to,
        subject_template=subject,
        html_template=template_str,
        environment={
            "project_name": settings.PROJECT_NAME,
            "username": username,
            "password": password,
            "email": email_to,
            "link": link,
        },
    )